var $gridList=$('#grid');
var   locationDetailInfoDialog,
		showLocationDetailInfo,
		editDialog;
require(['grid', 'topBar', 'layout'], function () {
	var postData={};
    $('body').layout();
    $('#top').topBar({
        left: [{
            type: 'filter-more',
            search: function () {
                if(getPostData()){
                    $('#grid').grid('setGridParam', {
                        datatype: "json",
                        postData: postData
                    }).trigger("reloadGrid");
                }
            },
            item: [
				{
				    text: '时间快捷',
				    name1: 'beginTime',
				    name2: 'endTime',
				    hidden:true,
				    type: 'dateLine'
				}
            ]
        }],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                        	exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                        	exportFile('Pdf');
                        }
                    }
                ]
            }
        ]
    });  
    
    function exportFile(suffix){
		if($('#grid').getGridParam("reccount") > 0){
			//var $postData=$("#top").parent().getValues();
    		var url = "/reportAbnorEnergy/exportReportAbnorEnergyStatis"+suffix+".htm";
			$.exportFile(url,postData);
		}else{
			$.messager.alert("警告","请先查询数据！","info");
		}
    }
    
    $gridList.grid({
		url : "/reportAbnorEnergy/searchReportAbnorEnergyStatis.htm",
		colModel : [ {name : "rcrdId", key: true, hidden: true, label:"rcrdId"},
		             {name : "beginLoLgt", key: true, hidden: true, label:"beginLoLgt"},
		             {name : "beginLoLtt", key: true, hidden: true, label:"beginLoLtt"},
		             {name : "beginLoDrc", key: true, hidden: true, label:"beginLoDrc"},
		             {name : "mftRpaTime", key: true, hidden: true, label:"mftRpaTime"},
		             {name : "licPltNo", sorttype : "text",align : "left", label:"车牌号"},
			{name : "drvName",sorttype : "text",align : "left", label:"驾驶员"},
			{name : "lineName",sorttype : "text",align : "left", label:"运行线路"},
			{name : "organName",sorttype : "text",align : "left", label:"所属机构"},
			{name : "carType",sorttype : "text",align : "left", label:"能源类型"},
			{name : "mftOccurTime",align : "center", label:"发生时间"},
			{name : "happenlocation",align : "left", label:"发生地点",
			formatter:function(cellvalue, options, rowObject){
				var licPltNo=rowObject.licPltNo;
				var drvName=rowObject.drvName;
				var beginLoLgt=rowObject.beginLoLgt;
				var beginLoLtt=rowObject.beginLoLtt;
				var beginLoDrc=rowObject.beginLoDrc;
				var spdingBeginTime=rowObject.mftOccurTime;
				var spdingEndTime=rowObject.mftRpaTime;
				var content = "<b><a href='javascript:void(0);' onclick='showLocationDetailInfo(\""+
						licPltNo+"\",\""+
						drvName+"\",\""+
						beginLoLgt+"\",\""+
						beginLoLtt+"\",\""+
						beginLoDrc+"\",\"异常报警查询\",\""+
						spdingBeginTime+"\",\""+
						spdingEndTime+"\")' style='color: blue;'>"+cellvalue+"</a></b>";
				return content;
			}},
//			{name : "location",index : "location",align : "left",
//				
//			},
		],
		multiselect:false,
		gridComplete : function() {
			var rowIds = $gridList.jqGrid('getDataIDs');
			var cells = $gridList.find('td[aria-describedby="grid_happenlocation"]');
			$.each(cells, function(i, obj){
				var title = $gridList.jqGrid('getCell', rowIds[i], 2)+","+$gridList.jqGrid('getCell', rowIds[i], 3);
				$(obj).attr('title', title);
			});
		}
	});

	//获取需要传递到后台的参数
	function getPostData(){
		type = 'DRIVER';
		var orgIds = commonFun.getLeftTreeCheckedNodes(type);
        if (orgIds == null || !orgIds.length) {
            $.messager.alert("提示", "请先选择左侧树的司机!", "warning");
            return false;
        }
        postData = $('#top').parent().getValues();
        postData.orgIds = orgIds.toString();
        postData.type = type;
        return true;
	};
	
	//-- 显示 详细位置信息
    showLocationDetailInfo = function (licPltNo ,drvName ,beginLoLgt ,beginLoLtt ,beginLoDrc ,type ,spedBeginTime ,spedEndTime) {
    	if(drvName=="undefined"){
			drvName="未知";
		}
    	var data = new Array();  // 在这里构造车辆数据的数组
    	var obj = new Object();  //车辆数据
    	obj.licPltNo = licPltNo; //车牌号
    	obj.driverName = drvName; //驾驶员
    	obj.loLgt = beginLoLgt; //经度
    	obj.loLtt = beginLoLtt; //纬度
    	obj.direction = beginLoDrc; //方向
    	obj.type = type; //异常类型 字符串表示
    	obj.beginTime = spedBeginTime; //开始时间
    	obj.endTime = spedEndTime; //结束时间
    	data.push(obj);
        openLocationDetailInfoDialog("超速位置查询", data); //弹出的标题和数据
    };
	
	function openLocationDetailInfoDialog(title, data) {
        if (!locationDetailInfoDialog) {
            locationDetailInfoDialog = $('<div>').dialog({
                title: title,
				url: './common/map/mapLocation.htm',
				height: $(window.parent).height() * 0.8,
				width: $(window.parent).width() * 0.85
            });
        }
        locationDetailInfoDialog.dialog('open');
        locationDetailInfoDialog.dialog('exec', 'showMapLocation', data);
    }
	
});